﻿using System;
using System.Collections.Generic;
using System.Text;

using Word = Microsoft.Office.Interop.Word;
using System.Windows.Forms;

namespace TemplateAddIn
{
    public class ImageCCCreator : ICreateContentControl
    {
        #region ICreateContentControl Members

        public bool CreateContentControl(Variable pVar, Microsoft.Office.Interop.Word.Range pRange, Microsoft.Office.Interop.Word._Application app)
        {
            object range = (object)pRange;

            try
            {
                Word.ContentControl cc = app.ActiveDocument.ContentControls.Add(pVar.VariableType, ref range);
                cc.Title = pVar.FullName;

                if (pVar.Content != null)
                {
                    try
                    {
                        ((Word.ContentControl)pVar.Content).Range.Copy();
                        cc.Range.Paste();
                    }
                    catch (Exception)
                    { }
                }

                pVar.ContentControlList.Add(cc);

                return true;
            }
            catch (Exception)
            {
                MessageBox.Show("You can´t insert a variable field inside another variable field.", "Warning!");

                return false;
            }
        }

        #endregion
    }
}
